package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.Department;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.entity.TrainingDemand;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.TrainingDemandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
public class TrainingDemandController {

    @Resource
    TrainingDemandService demandService;

    // 条件查询需求表
    @PostMapping("/demand/{pageIndex}/{pageSize}")
    public ResponseText selectDemand(
            @PathVariable("pageIndex")int pageIndex,
            @PathVariable("pageSize")int pageSize,
            @RequestBody TrainingDemand demand){
        ResponseText<PageBean<TrainingDemand>> responseText = null;
        try {

            PageBean<TrainingDemand> pageBean = demandService.selectTrainingDemandCondition(demand, pageIndex, pageSize);
            responseText = new ResponseText<>(pageBean,200,"查询成功！！！");

        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 删除需求
    @DeleteMapping("/demand/{did}")
    public ResponseText deleteDemand(@PathVariable("did")int did){

        ResponseText<PageBean<TrainingDemand>> responseText = null;

        try {

            demandService.deleteTrainingDemand(did);
            responseText = new ResponseText<>(null,200,"删除成功！！！");

        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }

        return responseText;
    }

    // 添加需求
    @PostMapping("/demand")
    public ResponseText addTrainingDemand(@RequestBody TrainingDemand demand){

        ResponseText responseText = null;
        try {
        demandService.addTrainingDemand(demand);
        responseText = new ResponseText(null,200,"添加成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 查询部门
    @GetMapping("/demand")
    public ResponseText getDep(){
        ResponseText responseText = null;
        try {
            List<Department> dep = demandService.getDep();
            responseText = new ResponseText(dep,200,"查询部门成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 修改
    @PutMapping("/demand")
    public ResponseText updateTrainingDemand(@RequestBody TrainingDemand demand){
        ResponseText responseText = null;
        try {
            demandService.updateTrainingDemand(demand);
            responseText = new ResponseText(null,200,"修改成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
